package beans;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;

import operations.Operations;

public class Meeting {
	private int id;
	private int id1;
	private int id2;
	private Cell position1;
	private Cell position2;
	private int timeStep;
	private State state;
	private List<Cell> nestToFood;
	private BigDecimal fraction1;
	private BigDecimal fraction2;

	public Meeting() {
		super();
	}

	public Meeting(int id, int id1, int id2, Cell position1, Cell position2, int timeStep, List<Cell> nestToFood) {
		super();
		this.id = id;
		this.id1 = id1;
		this.id2 = id2;
		this.position1 = position1;
		this.position2 = position2;
		this.timeStep = timeStep;
		this.nestToFood = nestToFood;
		setParameters();
	}

	private void setParameters() {
		if (nestToFood.contains(position1) && nestToFood.contains(position2))
			setState(State.ONLINE);
		else
			setState(State.OFFLINE);
		Cell cell = (Operations.getDistance(position1, nestToFood.get(0)) < Operations.getDistance(position2, nestToFood.get(0))) ? position1 : position2;
		int index1 = nestToFood.indexOf(cell);
		int length = nestToFood.size();
		setFraction1(divideBD(index1 + 1, length));
		setFraction2(new BigDecimal(1).subtract(getFraction1()));
	}

	public static BigDecimal divideBD(int num, int den) {
		BigDecimal bd_num = new BigDecimal(num);
		BigDecimal bd_den = new BigDecimal(den);
		return bd_num.divide(bd_den, MathContext.DECIMAL32).setScale(2, RoundingMode.HALF_EVEN);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId1() {
		return id1;
	}

	public void setId1(int id1) {
		this.id1 = id1;
	}

	public int getId2() {
		return id2;
	}

	public void setId2(int id2) {
		this.id2 = id2;
	}

	public Cell getPosition1() {
		return position1;
	}

	public void setPosition1(Cell position1) {
		this.position1 = position1;
	}

	public Cell getPosition2() {
		return position2;
	}

	public void setPosition2(Cell position2) {
		this.position2 = position2;
	}

	public int getTimeStep() {
		return timeStep;
	}

	public void setTimeStep(int timeStep) {
		this.timeStep = timeStep;
	}

	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

	public List<Cell> getNestToFood() {
		return nestToFood;
	}

	public void setNestToFood(List<Cell> nestToFood) {
		this.nestToFood = nestToFood;
	}

	public BigDecimal getFraction1() {
		return fraction1;
	}

	public void setFraction1(BigDecimal fraction1) {
		this.fraction1 = fraction1;
	}

	public BigDecimal getFraction2() {
		return fraction2;
	}

	public void setFraction2(BigDecimal fraction2) {
		this.fraction2 = fraction2;
	}

	@Override
	public String toString() {
		if (id1 < id2)
			return id + " [" + id1 + "-" + id2 + "] " + fraction2 + " at " + timeStep;
		else
			return id + " [" + id2 + "-" + id1 + "] " + fraction1 + " at " + timeStep;
	}

}
